var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var startPoint;
var endPoint;
function addPoint() {
		document.getElementById("tools").style.display="";
		
		google.maps.event.addListener(map, 'click', addPointFun);
}

var iclick = 0;
function addPointFun(event) {
	iclick++;
	if (iclick == 1) {
		var lt1 = event.latLng.lat();
		var lg1 = event.latLng.lng();
		startPoint = new google.maps.LatLng(lt1, lg1);
		// alert(event.latLng);
	}
	if (iclick == 2) {
		var lt2 = event.latLng.lat();
		var lg2 = event.latLng.lng();
		endPoint = new google.maps.LatLng(lt2, lg2);
		// alert(event.latLng);
		calcRoute();
	}
}

function clearPath() {
	document.getElementById("tools").style.display="none";
	directionsDisplay.setMap(null);
	google.maps.event.clearListeners(map, "click");
	startPoint = null;
	endPoint = null;
	iclick = 0;
}

function calcRoute() {
	var selectedMode = document.getElementById("mode").value;
	var request = {
		origin : startPoint,
		destination : endPoint,
		travelMode : google.maps.TravelMode[selectedMode]
	};
	directionsService.route(request, function(response, status) {
		if (status == google.maps.DirectionsStatus.OK) {
			directionsDisplay.setDirections(response);
		}
	});
	directionsDisplay.setMap(map);
}